//Global vars
var database = "wikiyetu.db";
var search_limit = 10;


function getArticle(input,evt) {
    if (input.value.length == 0 && document.getElementById("select-scheme").selectedIndex > 0) {
        return;
    }
    // Ignore the following keystrokes
    switch (evt.keyCode) {
        case 37: //left arrow
        case 39: //right arrow
        case 33: //page up  
        case 34: //page down  
        case 36: //home  
        case 35: //end
        //case 13: //enter
        case 9: //tab
        case 27: //esc
        case 16: //shift  
        case 17: //ctrl  
        case 18: //alt  
        case 20: //caps lock
        case 8: //backspace  
        case 46: //delete 
        case 38: //up arrow 
        case 40: //down arrow
            return;
        break;
    }
    	if (evt.keyCode == 13){ 
		scheme=document.getElementById("select-scheme").selectedIndex;
		switch(scheme){
			case 0: //randomly
				wikiyetuSQLByRand();
				break;
			case 1: //by page title
				wikiyetuSQLByTitle(input.value);
				break;
			case 2: //by ID
				wikiyetuSQLByID(input.value);
				break;
			case 3: //by Article text
				wikiyetuSQLByArticle(input.value);
				break;
		}
		//alert(document.getElementById("select-scheme").selectedIndex);
    		//wikiyetuSQL(input.value);
	}
}

function wikify(wikicontent)
{
        return Wiky.toHtml(wikicontent);
}


function getTableCardinality() {
        // get the storage service
        var store = Components.classes["@mozilla.org/storage/service;1"].
        getService(Components.interfaces.mozIStorageService);

        // get the profile directory
        var file = Components.classes["@mozilla.org/file/directory_service;1"]
        .getService(Components.interfaces.nsIProperties)
        .get("ProfD", Components.interfaces.nsILocalFile);
        // This makes file point to PROFILE_DIR/foo.sdb
        file.append(database);
        var db = store.openDatabase(file);
        // SQL that we want data from
        var statement =
        Components.classes['@mozilla.org/storage/statement-wrapper;1']
        .createInstance(Components.interfaces.mozIStorageStatementWrapper);
        var cardinality;
        try {
                statement.initialize(db.createStatement("select count(*) as cardinality from text"));
                while(statement.step()) {
                       cardinality=statement.row.cardinality;
                }
        } finally {
                statement.reset();
        }
	return cardinality;
}


function wikiyetuSQLByID(identity) {
	// get the storage service
	var store = Components.classes["@mozilla.org/storage/service;1"].
	getService(Components.interfaces.mozIStorageService);
	
	// get the profile directory
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
	.getService(Components.interfaces.nsIProperties)
	.get("ProfD", Components.interfaces.nsILocalFile);
	// This makes file point to PROFILE_DIR/foo.sdb
	file.append(database);
	var db = store.openDatabase(file);
	// SQL that we want data from
	var statement =
	Components.classes['@mozilla.org/storage/statement-wrapper;1']
	.createInstance(Components.interfaces.mozIStorageStatementWrapper);
	
	try {
		sql = "select old_id as pageid, (select page_title from page where page_latest= old_id) as pagetitle,old_text from text where old_id = "+identity+" limit 1";
		//statement.initialize(db.createStatement("select old_id as pageid, (select page_title from page where page_latest= old_id) as pagetitle,old_text from text where old_id = "+identity+" limit 1"));
		statement.initialize(db.createStatement(sql));
		while(statement.step()) {
			content.document.getElementById("articleTitle").innerHTML="<h1>"+statement.row.pagetitle+"</h1>"+"<br/><h3>"+statement.row.pageid+"</h3>";
			content.document.getElementById("articleText").innerHTML=wikify(statement.row.old_text);
		}
	} finally {
		statement.reset();
	}
}

function wikiyetuSQLByArticle(searchtext) {
	// get the storage service
	var store = Components.classes["@mozilla.org/storage/service;1"].
	getService(Components.interfaces.mozIStorageService);
	
	// get the profile directory
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
	.getService(Components.interfaces.nsIProperties)
	.get("ProfD", Components.interfaces.nsILocalFile);
	// This makes file point to PROFILE_DIR/foo.sdb
	file.append(database);
	var db = store.openDatabase(file);
	// SQL that we want data from
	var statement =
	Components.classes['@mozilla.org/storage/statement-wrapper;1']
	.createInstance(Components.interfaces.mozIStorageStatementWrapper);
	
	try {
		sql = "select (select page_title from page where page_latest= old_id) as pagetitle ,substr(old_text,0,100) as snippet, old_id as pageid from text where old_text like '%"+searchtext+"%' limit "+search_limit;
		statement.initialize(db.createStatement(sql));

		content.document.getElementById("articleTitle").innerHTML="<h1>Search Results</h1>"+"<br /> for <h3>"+searchtext+"</h3>";
		content.document.getElementById("articleText").innerHTML = "";
		while(statement.step()) {
			content.document.getElementById("articleText").innerHTML = content.document.getElementById("articleText").innerHTML+"<h2>"+statement.row.pageid+" : "+wikify(statement.row.pagetitle)+"</h2><br/>"+wikify(statement.row.snippet);
		}
	} finally {
		statement.reset();
	}
}

function wikiyetuSQLByTitle(searchtext) {
	// get the storage service
	var store = Components.classes["@mozilla.org/storage/service;1"].
	getService(Components.interfaces.mozIStorageService);
	
	// get the profile directory
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
	.getService(Components.interfaces.nsIProperties)
	.get("ProfD", Components.interfaces.nsILocalFile);
	// This makes file point to PROFILE_DIR/foo.sdb
	file.append(database);
	var db = store.openDatabase(file);
	// SQL that we want data from
	var statement =
	Components.classes['@mozilla.org/storage/statement-wrapper;1']
	.createInstance(Components.interfaces.mozIStorageStatementWrapper);
	
	try {
		sql = "select page_title as pagetitle , (select substr(old_text,0,100)  from text where page_latest= old_id) as snippet, page_latest as pageid from page where pagetitle like '%"+searchtext+"%' limit "+search_limit;
                statement.initialize(db.createStatement(sql)); 
                content.document.getElementById("articleTitle").innerHTML="<h1>Search Results</h1>"+"<br /> for <h3>"+searchtext+"</h3>";
		content.document.getElementById("articleText").innerHTML = "";
                while(statement.step()) {
                	content.document.getElementById("articleText").innerHTML = content.document.getElementById("articleText").innerHTML+"<h2>"+statement.row.pageid+" : "+wikify(statement.row.pagetitle)+"</h2><br/>"+wikify(statement.row.snippet);
                }

	} finally {
		statement.reset();
	}
}

function wikiyetuSQLByRand(identity) {
	// get the storage service
	var store = Components.classes["@mozilla.org/storage/service;1"].
	getService(Components.interfaces.mozIStorageService);
	
	// get the profile directory
	var file = Components.classes["@mozilla.org/file/directory_service;1"]
	.getService(Components.interfaces.nsIProperties)
	.get("ProfD", Components.interfaces.nsILocalFile);
	// This makes file point to PROFILE_DIR/foo.sdb
	file.append(database);
	var db = store.openDatabase(file);
	// SQL that we want data from
	var statement =
	Components.classes['@mozilla.org/storage/statement-wrapper;1']
	.createInstance(Components.interfaces.mozIStorageStatementWrapper);
	//Get the total number of rows in the table and get a random no < total no of rows. Show that row
	var randomnumber=Math.floor(Math.random()*getTableCardinality());
	try {
		sql = "select (select page_title from page where page_latest= old_id) as pagetitle, old_text, old_id as pageid from text limit "+randomnumber+",1";
		statement.initialize(db.createStatement(sql));
		while(statement.step()) {
			content.document.getElementById("articleTitle").innerHTML= "<h1>"+statement.row.pagetitle+"</h1>"+"<br/><h3>"+statement.row.pageid+"</h3>";
			content.document.getElementById("articleText").innerHTML=wikify(statement.row.old_text);
		}
	} finally {
		statement.reset();
	}
}
